import sequelize from '../config/database.js';
import User from './user.js';
import Friendship from './friendship.js';
import Call from './call.js';
import initAdminUserModel from './AdminUser.js';
import initServiceStaffModel from './ServiceStaff.js';
import initDefaultAvatarModel from './DefaultAvatar.js';

// 初始化所有模型
User.initModel(sequelize);
Friendship.initModel(sequelize);
Call.initModel(sequelize);
const AdminUser = initAdminUserModel(sequelize);
const ServiceStaff = initServiceStaffModel(sequelize);
const DefaultAvatar = initDefaultAvatarModel(sequelize);

// 建立模型之间的关联关系
Friendship.associate({ User });

// 用户和好友之间的多对多关系
User.belongsToMany(User, {
  through: Friendship,
  as: 'friends',
  foreignKey: 'userId',
  otherKey: 'friendId'
});

User.belongsToMany(User, {
  through: Friendship,
  as: 'friendOf',
  foreignKey: 'friendId',
  otherKey: 'userId'
});

// 用户和通话记录的关联
Call.belongsTo(User, { 
  as: 'caller',
  foreignKey: 'callerId'
});

Call.belongsTo(User, {
  as: 'receiver',
  foreignKey: 'receiverId'
});

User.hasMany(Call, { 
  as: 'outgoingCalls',
  foreignKey: 'callerId'
});

User.hasMany(Call, {
  as: 'incomingCalls',
  foreignKey: 'receiverId'
});

export { User, Friendship, Call, AdminUser, ServiceStaff, DefaultAvatar };
